{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Conduct a Vector Query\n",
    "\n",
    "> This topic describes how to conduct a vector query."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Connect to milvus."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Milvus.Client, 2.2.2-preview.2</span></li></ul></div></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>{MilvusClient:localhost:19530}</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Address</td><td><div class=\"dni-plaintext\"><pre>localhost:19530</pre></div></td></tr></tbody></table></div></details><style>\r\n",
       ".dni-code-hint {\r\n",
       "    font-style: italic;\r\n",
       "    overflow: hidden;\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview {\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview td {\r\n",
       "    vertical-align: top;\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "details.dni-treeview {\r\n",
       "    padding-left: 1em;\r\n",
       "}\r\n",
       "table td {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "table tr { \r\n",
       "    vertical-align: top; \r\n",
       "    margin: 0em 0px;\r\n",
       "}\r\n",
       "table tr td pre \r\n",
       "{ \r\n",
       "    vertical-align: top !important; \r\n",
       "    margin: 0em 0px !important;\r\n",
       "} \r\n",
       "table th {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "</style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#r \"nuget: Milvus.Client, 2.2.2-preview.2\"\n",
    "#!import config/Settings.cs\n",
    "\n",
    "using Milvus.Client;\n",
    "using InteractiveKernel = Microsoft.DotNet.Interactive.Kernel;\n",
    "\n",
    "//Connect to milvus\n",
    "(string endpoint, int port, string userName, string password) = Settings.LoadFromFile();\n",
    "\n",
    "MilvusClient milvusClient = default;\n",
    "\n",
    "milvusClient = new MilvusClient(endpoint, port, userName, password, null);\n",
    "milvusClient"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load collection\n",
    "\n",
    "🍺🍺🍺🍺🍺🍺🍺🍺🍺🍺🍺🍺🍺🍺🍺🍺🍺🍺\n",
    "\n",
    "All search and query operations within Milvus are executed in memory. Load the collection to memory before conducting a vector query."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>Milvus.Client.MilvusCollectionDescription</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Aliases</td><td><div class=\"dni-plaintext\"><pre>[  ]</pre></div></td></tr><tr><td>CollectionName</td><td><div class=\"dni-plaintext\"><pre>book</pre></div></td></tr><tr><td>CollectionId</td><td><div class=\"dni-plaintext\"><pre>443732094175944322</pre></div></td></tr><tr><td>ConsistencyLevel</td><td><span>Session</span></td></tr><tr><td>CreationTimestamp</td><td><div class=\"dni-plaintext\"><pre>1692705169260</pre></div></td></tr><tr><td>Schema</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>Milvus.Client.CollectionSchema</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Name</td><td><div class=\"dni-plaintext\"><pre>book</pre></div></td></tr><tr><td>Description</td><td><div class=\"dni-plaintext\"><pre></pre></div></td></tr><tr><td>Fields</td><td><table><thead><tr><th><i>index</i></th><th>value</th></tr></thead><tbody><tr><td>0</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>Milvus.Client.FieldSchema</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Name</td><td><div class=\"dni-plaintext\"><pre>book_id</pre></div></td></tr><tr><td>DataType</td><td><span>Int64</span></td></tr><tr><td>IsPrimaryKey</td><td><div class=\"dni-plaintext\"><pre>True</pre></div></td></tr><tr><td>AutoId</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>IsPartitionKey</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>Description</td><td><div class=\"dni-plaintext\"><pre></pre></div></td></tr><tr><td>IsDynamic</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>MaxLength</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr><tr><td>Dimension</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr><tr><td>State</td><td><span>FieldCreated</span></td></tr><tr><td>FieldId</td><td><div class=\"dni-plaintext\"><pre>100</pre></div></td></tr></tbody></table></div></details></td></tr><tr><td>1</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>Milvus.Client.FieldSchema</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Name</td><td><div class=\"dni-plaintext\"><pre>word_count</pre></div></td></tr><tr><td>DataType</td><td><span>Int64</span></td></tr><tr><td>IsPrimaryKey</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>AutoId</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>IsPartitionKey</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>Description</td><td><div class=\"dni-plaintext\"><pre></pre></div></td></tr><tr><td>IsDynamic</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>MaxLength</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr><tr><td>Dimension</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr><tr><td>State</td><td><span>FieldCreated</span></td></tr><tr><td>FieldId</td><td><div class=\"dni-plaintext\"><pre>101</pre></div></td></tr></tbody></table></div></details></td></tr><tr><td>2</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>Milvus.Client.FieldSchema</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Name</td><td><div class=\"dni-plaintext\"><pre>book_name</pre></div></td></tr><tr><td>DataType</td><td><span>VarChar</span></td></tr><tr><td>IsPrimaryKey</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>AutoId</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>IsPartitionKey</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>Description</td><td><div class=\"dni-plaintext\"><pre></pre></div></td></tr><tr><td>IsDynamic</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>MaxLength</td><td><div class=\"dni-plaintext\"><pre>256</pre></div></td></tr><tr><td>Dimension</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr><tr><td>State</td><td><span>FieldCreated</span></td></tr><tr><td>FieldId</td><td><div class=\"dni-plaintext\"><pre>102</pre></div></td></tr></tbody></table></div></details></td></tr><tr><td>3</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>Milvus.Client.FieldSchema</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Name</td><td><div class=\"dni-plaintext\"><pre>book_intro</pre></div></td></tr><tr><td>DataType</td><td><span>FloatVector</span></td></tr><tr><td>IsPrimaryKey</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>AutoId</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>IsPartitionKey</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>Description</td><td><div class=\"dni-plaintext\"><pre></pre></div></td></tr><tr><td>IsDynamic</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td>MaxLength</td><td><div class=\"dni-plaintext\"><pre>&lt;null&gt;</pre></div></td></tr><tr><td>Dimension</td><td><div class=\"dni-plaintext\"><pre>2</pre></div></td></tr><tr><td>State</td><td><span>FieldCreated</span></td></tr><tr><td>FieldId</td><td><div class=\"dni-plaintext\"><pre>103</pre></div></td></tr></tbody></table></div></details></td></tr></tbody></table></td></tr><tr><td>EnableDynamicFields</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr></tbody></table></div></details></td></tr><tr><td>ShardsNum</td><td><div class=\"dni-plaintext\"><pre>1</pre></div></td></tr><tr><td>StartPositions</td><td><i>(empty)</i></td></tr></tbody></table></div></details><style>\r\n",
       ".dni-code-hint {\r\n",
       "    font-style: italic;\r\n",
       "    overflow: hidden;\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview {\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview td {\r\n",
       "    vertical-align: top;\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "details.dni-treeview {\r\n",
       "    padding-left: 1em;\r\n",
       "}\r\n",
       "table td {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "table tr { \r\n",
       "    vertical-align: top; \r\n",
       "    margin: 0em 0px;\r\n",
       "}\r\n",
       "table tr td pre \r\n",
       "{ \r\n",
       "    vertical-align: top !important; \r\n",
       "    margin: 0em 0px !important;\r\n",
       "} \r\n",
       "table th {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "</style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "string collectionName = \"book\";\n",
    "MilvusCollection collection = milvusClient.GetCollection(collectionName);\n",
    "\n",
    "if(!(await milvusClient.HasCollectionAsync(collectionName)))\n",
    "{\n",
    "    Console.WriteLine($\"Collection {collectionName} not exist\");\n",
    "    return;\n",
    "}\n",
    "\n",
    "await collection.LoadAsync();\n",
    "\n",
    "//Waiting for collection loaded\n",
    "collection.WaitForCollectionLoadAsync(timeout: TimeSpan.FromSeconds(10));\n",
    "\n",
    "MilvusCollectionDescription collectionInfo = await collection.DescribeAsync();\n",
    "collectionInfo"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Conduct a vector query\n",
    "\n",
    "The following example filters the vectors with certain book_id values, and returns the book_id field and book_intro of the results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table><thead><tr><th><i>index</i></th><th>value</th></tr></thead><tbody><tr><td>0</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>Field: {FieldName: book_id, DataType: Int64, Data: 4, RowCount: 4}</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Data</td><td><div class=\"dni-plaintext\"><pre>[ 2, 4, 6, 8 ]</pre></div></td></tr><tr><td>RowCount</td><td><div class=\"dni-plaintext\"><pre>4</pre></div></td></tr><tr><td>FieldName</td><td><div class=\"dni-plaintext\"><pre>book_id</pre></div></td></tr><tr><td>FieldId</td><td><div class=\"dni-plaintext\"><pre>0</pre></div></td></tr><tr><td>DataType</td><td><span>Int64</span></td></tr><tr><td>IsDynamic</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr></tbody></table></div></details></td></tr><tr><td>1</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>Field: {FieldName: word_count, DataType: Int64, Data: 4, RowCount: 4}</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Data</td><td><div class=\"dni-plaintext\"><pre>[ 10002, 10004, 10006, 10008 ]</pre></div></td></tr><tr><td>RowCount</td><td><div class=\"dni-plaintext\"><pre>4</pre></div></td></tr><tr><td>FieldName</td><td><div class=\"dni-plaintext\"><pre>word_count</pre></div></td></tr><tr><td>FieldId</td><td><div class=\"dni-plaintext\"><pre>0</pre></div></td></tr><tr><td>DataType</td><td><span>Int64</span></td></tr><tr><td>IsDynamic</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr></tbody></table></div></details></td></tr></tbody></table><style>\r\n",
       ".dni-code-hint {\r\n",
       "    font-style: italic;\r\n",
       "    overflow: hidden;\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview {\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview td {\r\n",
       "    vertical-align: top;\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "details.dni-treeview {\r\n",
       "    padding-left: 1em;\r\n",
       "}\r\n",
       "table td {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "table tr { \r\n",
       "    vertical-align: top; \r\n",
       "    margin: 0em 0px;\r\n",
       "}\r\n",
       "table tr td pre \r\n",
       "{ \r\n",
       "    vertical-align: top !important; \r\n",
       "    margin: 0em 0px !important;\r\n",
       "} \r\n",
       "table th {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "</style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "string expr = \"book_id in [2,4,6,8]\";\n",
    "\n",
    "QueryParameters queryParameters = new ();\n",
    "queryParameters.OutputFields.Add(\"book_id\");\n",
    "queryParameters.OutputFields.Add(\"word_count\");\n",
    "\n",
    "IReadOnlyList<FieldData> queryResult = await collection.QueryAsync(\n",
    "    expr,\n",
    "    queryParameters);\n",
    "\n",
    "queryResult"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| Parameter | Description |\n",
    "| --------- | ----------- |\n",
    "| output_fields (optional) | List of names of the fields to return. |\n",
    "| vectors | Vectors to query. |\n",
    "| expr | Boolean expression used to filter attribute. Find more expression details in Boolean Expression Rules. |"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
